﻿using MangoMes.Commont.DapperHelps;
using MangoMes.Commont.Domain.Productions;
using MangoMes.Commont.DTOs;
using MangoMes.Commont.Infrastructures.Interfaces;
using MediatR;

namespace MangoMes.Write.API.Applications.Strippings
{
    public class StrippingSeachHandler : IRequestHandler<StrippingSeachCommand, PageDTO<StrippingDto>>
    {
        #region 仓储
        private readonly IBateRepository<StrippingDto> StrippingDtoRepository;
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="StrippingDtoRepository"></param>
        public StrippingSeachHandler(IBateRepository<StrippingDto> StrippingDtoRepository)
        {
            this.StrippingDtoRepository = StrippingDtoRepository;
        }
        #endregion


        #region 生产退料列表
        /// <summary>
        /// 生产退料 列表
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<PageDTO<StrippingDto>> Handle(StrippingSeachCommand request, CancellationToken cancellationToken)
        {
            //异常处理
            try
            {
                //初始化一个ResultDTO<StrippingDto>对象result，并设置Code为 0
                PageDTO<StrippingDto> result = new PageDTO<StrippingDto>();

                var pageNum = (request.pageIndex - 1) * request.pageSize;
                //创建DapperHelper对象 用于与数据库进行交互
                DapperHelper dapperHelper = new DapperHelper();
                StrippingDto dto = new StrippingDto();

                //连接mySql 
                //定义一个字符串变量conn  将dapperHelper对象的MangoMesWrite属性的值赋给它
                string conn = dapperHelper.MangoMesWrite;

                //sql语句
                string str = $"select * from stripping a JOIN productionworkorders b on a.ProductionWorkOrdersNo=b.ProductionWorkOrdersNo join warehouselist c on a.StashId=c.StashId";

                var list = StrippingDtoRepository.GetAll(str, dto, conn).ToList();

                if (!string.IsNullOrEmpty(request.StrippingNode))
                {
                    list = list.Where(x => x.StrippingNode.Contains(request.StrippingNode)).ToList();
                }
                if (!string.IsNullOrEmpty(request.ProductionWorkOrdersNo))
                {
                    list = list.Where(x => x.ProductionWorkOrdersNo.Contains(request.ProductionWorkOrdersNo)).ToList();
                }
                if (!string.IsNullOrEmpty(request.StashName))
                {
                    list = list.Where(x => x.StashName.Contains(request.StashName)).ToList();
                }

                result.TotalCount = list.Count(); //计算list列表中元素个数
                result.PageCount = (int)Math.Ceiling((list.Count() * 1.0) / request.pageSize); //表示总的记录数量
                list = list.OrderBy(x => x.StrippingId).Skip((request.pageIndex - 1) * request.pageSize).Take(request.pageSize).ToList();  //计算总页数
                result.Data = list; 

                return result;
            }
            catch (Exception)
            {

                throw;
            }
        }
        #endregion

    }
}
